package com.brainardphotography.blog.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public final class Utils {
	public static SimpleDateFormat RFC822DATEFORMAT = new SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
	
	public static Date createDate(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

		try {
			return sdf.parse(dateString);
		} catch (ParseException ex) {
			// TODO: Figure out best way to handle exceptions.
			throw new IllegalArgumentException("Date string is wrong format. Needs to be MM/DD/YYYY", ex);
		}
	}
	
	public static String formatDateAsRFC822(Date date) {
		return RFC822DATEFORMAT.format(date);
	}

	/**
	 * Create a new date from the given year, month and day strings.
	 * 
	 * @param year
	 * @param month 1 based month value (1=Jan, 2=Feb, ... 12=Dec)
	 * @param day
	 * @return
	 */
	public static Date createDate(String year, String month, String day) {
		return createDate(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
	}

	/**
	 * Create a new date from the given year, month and day values.
	 * 
	 * @param year
	 * @param month 1 based month value (1=Jan, 2=Feb, ... 12=Dec)
	 * @param day
	 * @return
	 */
	public static Date createDate(int year, int month, int day) {
		Calendar cal = Calendar.getInstance();

		cal.set(year, month - 1, day, 0, 0, 0);
		return cal.getTime();
	}
	
	public static Date getTomorrow(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		cal.add(Calendar.DATE, 1);
		
		return cal.getTime();
	}
	
	public static boolean isStringBlank(String str) {
		return str == null || "".equals(str.trim());
	}
	
	/**
	 * There is no need for this class to be instantiated.
	 */
	private Utils() {}
}
